package com.hengtiansoft.lfy.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 测评数据库
 */

public class EvaluatorDBHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static EvaluatorDBHelper instance;
    //数据库名
    public static final String DB_EVALUATOR_NAME = "evaluator";
    //信息表
    public static final String EVALUATOR_TABLE_NAME = "evaluator";
    //id
    public static final String EVALUATOR_ID = "id";
    //英文
    public static final String EVALUATOR_ENGLISH = "english";
    //中文
    public static final String EVALUATOR_CHINESE = "chinese";
    //模块类型
    public static final String EVALUATOR_TYPE = "type";
    //每句的分数
    public static final String EVALUATOR_SCORE = "score";
    //是否练习过
    public static final String EVALUATOR_STATE = "state";
    //每个单词的分数
    public static final String EVALUATOR_WORD_SCORE = "word_score";
    //本地语音
    public static final String EVALUATOR_LOCAL_VOICE = "local_voice";
    //预备字段
    public static final String EVALUATOR_NOTE = "note";
    //服务器对应的模块表编号
    public static final String EVALUATOR_MODE_SERIAL = "serial";
    //服务器编号
    public static final String EVALUATOR_MODE_PKSERIAL = "pk_serial";
    //写入时间
    public static final String EVALUATOR_TIME = "time";
    //试卷类型 口语/翻译
    public static final String EVALUATOR_MODE_TYPE = "mode_type";


    //模块表
    public static final String MODE_TABLE_NAME = "evaluator_mode";
    //id
    public static final String MODE_ID = "id";
    //模块名称
    public static final String MODE_NAME = "name";
    //模块图URL
    public static final String MODE_IMAGURL = "imageurl";

    //服务器编号
    public static final String MODE_SERIAL = "serial";
    //总分数
    public static final String MODE_SCORE = "score";
    //写入时间
    public static final String MODE_TIME = "time";
    //更新时间
    public static final String MODE_UPDATE_TIME = "updatetime";
    //预备字段
    public static final String MODE_NOTE = "note";
    //是否过期 0过期 1未过期
    public static final String MODE_STATE = "state";
    //练习时长 秒
    public static final String MODE_EXERCISE_TIME = "exertisetime";
    //练习类型 口语/翻译
    public static final String MODE_EXERCISE_TYPE = "type";



    private static final String EVALUATOR_TABLE_CREATE = "CREATE TABLE "
            + EVALUATOR_TABLE_NAME + " (" + EVALUATOR_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + EVALUATOR_ENGLISH + " TEXT, "
            + EVALUATOR_CHINESE + " TEXT, "
            + EVALUATOR_TYPE + " TEXT, "
            + EVALUATOR_SCORE + " TEXT, "
            + EVALUATOR_STATE + " TEXT, "
            + EVALUATOR_WORD_SCORE + " TEXT, "
            + EVALUATOR_LOCAL_VOICE + " TEXT, "
            + EVALUATOR_MODE_SERIAL + " TEXT, "
            + EVALUATOR_TIME + " TEXT, "
            + EVALUATOR_MODE_TYPE + " TEXT, "
            + EVALUATOR_MODE_PKSERIAL + " TEXT, "
            + EVALUATOR_NOTE + " TEXT); ";

    private static final String MODE_TABLE_CREATE = "CREATE TABLE "
            + MODE_TABLE_NAME + " ("
            + MODE_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + MODE_NAME + " TEXT, "
            + MODE_IMAGURL + " TEXT, "
            + MODE_SCORE + " TEXT, "
            + MODE_SERIAL + " TEXT, "
            + MODE_TIME + " TEXT, "
            + MODE_STATE + " TEXT, "
            + MODE_EXERCISE_TIME + " TEXT, "
            + MODE_UPDATE_TIME+ " TEXT, "
            + MODE_EXERCISE_TYPE+ " TEXT, "
            + MODE_NOTE + " TEXT);";


    private EvaluatorDBHelper(Context context) {
        super(context, DB_EVALUATOR_NAME, null, DATABASE_VERSION);
    }

    public static EvaluatorDBHelper getInstance(Context context) {
        if (instance == null) {
            instance = new EvaluatorDBHelper(context.getApplicationContext());
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(EVALUATOR_TABLE_CREATE);
        db.execSQL(MODE_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void closeDB() {
        if (instance != null) {
            try {
                SQLiteDatabase db = instance.getWritableDatabase();
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            instance = null;
        }
    }

}
